package com.space.domain.core;

import com.space.components.domain.core.Repository;
import com.space.components.domain.core.diff.ArrayDiff;
import com.space.components.domain.core.diff.EntityDiff;
import com.space.domain.core.entity.UserId;

/**
 * @author <a href="mailto:414563035@qq.com">Bedroom</a>
 * @date 2021/6/2 5:48 下午
 */
public class RepositoryImpl implements Repository<User, UserId> {
    @Override
    public User find(UserId userId) {
        return null;
    }

    @Override
    public void remove(User aggregate) {

    }

    @Override
    public void save(User aggregate) {
        EntityDiff<User> diff = aggregate.diff();
        if (diff.isSelfModified()) {
            User diffUser = diff.toSelfDiffEntity();
        }
        // 第一种方式
        ArrayDiff<User> childrenDiff = diff.getArrayDiff("children");
        childrenDiff
                .added(newValue -> {
                    System.out.println("--1--");
                    handle(newValue);
                })
                .modified(diffValue -> {

                })
                .removed(oldValue -> {

                })
                .execute();
        // 第二种方式
        diff.getArrayDiff("children", User.class)
                .added(newValue -> {
                    System.out.println("--2--");
                    handle(newValue);
                })
                .modified(diffValue -> {

                })
                .removed(oldValue -> {

                })
                .execute();
    }

    private void handle(User user) {
        System.out.println(user);
    }

}
